package inet.app.view.adapter;

import inet.app.R;
import inet.app.model.object.ContactModel;
import inet.app.stickylistheaders.StickyListHeadersAdapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.SectionIndexer;
import android.widget.TextView;

public class ContactAdapter extends ArrayAdapter<ContactModel> implements
		StickyListHeadersAdapter, SectionIndexer {

	private Context mContext;
	private List<ContactModel> mContactModels;
	private List<Boolean> mSelector;
	private int[] mSectionIndices;
	private Character[] mSectionLetters;
	private LayoutInflater mInflater;

	public ContactAdapter(Context context, int textViewResourceId,
			List<ContactModel> objects, List<Boolean> selector) {

		super(context, textViewResourceId, objects);
		this.mContext = context;
		this.mContactModels = objects;
		this.mSelector = selector;
		this.mInflater = LayoutInflater.from(context);
		this.mSectionIndices = getSectionIndices();
		this.mSectionLetters = getSectionLetters();
	}

	@Override
	public int getCount() {
		if (mContactModels == null) {
			return 0;
		}
		return mContactModels.size();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder holder = null;
		final ContactModel model = getItem(position);

		if (convertView == null) {
			holder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.item_contact, null);
			holder.nameTextView = (TextView) convertView
					.findViewById(R.id.name_textview);
			holder.backgroundLayout = (LinearLayout) convertView
					.findViewById(R.id.background_layout);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		if (mSelector.get(position)) {
			holder.backgroundLayout.setBackgroundResource(R.color.orange);
			holder.nameTextView.setTextColor(mContext.getResources().getColor(
					R.color.white));
		} else {
			holder.backgroundLayout.setBackgroundResource(R.color.white);
			holder.nameTextView.setTextColor(mContext.getResources().getColor(
					R.color.main_red_color));
		}
		holder.nameTextView.setText(model.getContactName());
		return convertView;
	}

	/************************** Header row *******************************/

	private int[] getSectionIndices() {

		ArrayList<Integer> sectionIndices = new ArrayList<Integer>();

		// get first char of first contact
		ContactModel contact = mContactModels.get(0);
		if (contact == null) {
			contact = new ContactModel();
		}
		String contactName = contact.getContactName();
		char lastFirstChar = contactName.charAt(0);
		sectionIndices.add(0);

		// get list first char of all contact
		int size = mContactModels.size();
		for (int i = 1; i < size; i++) {
			contact = mContactModels.get(i);
			if (contact == null) {
				contact = new ContactModel();
			}
			contactName = contact.getContactName();
			if (contactName.charAt(0) != lastFirstChar) {
				lastFirstChar = contactName.charAt(0);
				sectionIndices.add(i);
			}
		}

		int[] sections = new int[sectionIndices.size()];
		for (int i = 0; i < sectionIndices.size(); i++) {
			sections[i] = sectionIndices.get(i);
		}
		return sections;
	}

	private Character[] getSectionLetters() {
		Character[] letters = new Character[mSectionIndices.length];
		for (int i = 0; i < mSectionIndices.length; i++) {
			letters[i] = mContactModels.get(mSectionIndices[i])
					.getContactName().charAt(0);
		}
		return letters;
	}

	@Override
	public int getPositionForSection(int section) {
		if (section >= mSectionIndices.length) {
			section = mSectionIndices.length - 1;
		} else if (section < 0) {
			section = 0;
		}
		return mSectionIndices[section];
	}

	@Override
	public int getSectionForPosition(int position) {
		for (int i = 0; i < mSectionIndices.length; i++) {
			if (position < mSectionIndices[i]) {
				return i - 1;
			}
		}
		return mSectionIndices.length - 1;
	}

	@Override
	public Object[] getSections() {
		return mSectionLetters;
	}

	@Override
	public View getHeaderView(int position, View convertView, ViewGroup parent) {
		HeaderViewHolder holder;
		if (convertView == null) {
			holder = new HeaderViewHolder();
			convertView = mInflater.inflate(R.layout.contact_header, parent,
					false);
			holder.text = (TextView) convertView.findViewById(R.id.letter);
			convertView.setTag(holder);
		} else {
			holder = (HeaderViewHolder) convertView.getTag();
		}
		String headerText = mContactModels.get(position).getContactName()
				.substring(0, 1);
		holder.text.setText(headerText);
		return convertView;
	}

	@Override
	public long getHeaderId(int position) {
		return mContactModels.get(position).getContactName().charAt(0);
	}

	static class ViewHolder {
		public LinearLayout backgroundLayout;
		public TextView nameTextView;
	}

	static class HeaderViewHolder {
		TextView text;
	}

}